<template>
  <div id="wechatticket">
    <img src="../assets/images/anyloading.gif" class="logo" style="height:180px;">
  </div>
</template>

<script>
  export default{
    name: 'wechatticket',
    components: {
    },
    data () {
      return {
        priasePath: sessionStorage.getItem('praisePath') || ''
      }
    },
    created: function () {
      // 获取会员卡号
      this.$axios.post(this.apidomain + '/api/card/getWxCardId/' + this.getCookie('appid'), {
        cardType: 'MEMBER_CARD'
      })
      .then((response) => {
        const timestamp = Date.parse(new Date()) / 1000
        const nonceStr = Math.random().toString(36).substr(2)
        this.getObjDetail(this.apidomain + '/api/wechat/mp/getCardApiTicket/' + this.getCookie('appid'))
        .then((data) => {
          const noncearr = [timestamp, response.data.data.cardId, data.data, nonceStr]
          noncearr.sort()
          const signature = this.$jssha(noncearr.join(''))
          this.$wechat.ready(() => {
            this.$wechat.addCard({
              cardList: [{
                cardId: response.data.data.cardId,
                cardExt: '{"timestamp": "' + timestamp + '", "nonce_str": "' + nonceStr + '", "signature": "' + signature + '"}'
              }],
              success: (res) => {
                console.log(res)
                if (this.priasePath !== '') { // 感恩礼
                  // this.routeto(this.priasePath, {
                  //   oid: this.$route.query.oid || '',
                  //   mid: this.$route.query.mid || '',
                  //   brandid: this.$route.query.brandid || '1',
                  //   sid: this.$route.query.sid || ''
                  // })
                  window.WeixinJSBridge.invoke('closeWindow')
                } else {
                  // 判断是否需要跳到完善资料
                  this.$axios.post(this.apidomain + '/api/cust/getCustInfo/' + this.getCookie('appid'), {
                    wxOpenId: this.getCookie('openid')
                  })
                  .then((custdata) => {
                    if (custdata.data.data !== null) {
                      if (custdata.data.data.vip === '0') {
                        this.routeto('usersetting')
                      } else {
                        this.routeto('home')
                      }
                    } else {
                      this.routeto('usersetting')
                    }
                  })
                }
              },
              cancel: (error) => {
                console.log(error)
                if (this.priasePath !== '') {
                  window.alert('亲爱的用户，您还没有激活会员卡哦！')
                  window.WeixinJSBridge.invoke('closeWindow')
                } else {
                  window.WeixinJSBridge.invoke('closeWindow')
                }
                // 监听是否点击关闭原生卡
                document.addEventListener('visibilitychange', () => {
                  if (document.visibilityState !== 'hidden') {
                    window.WeixinJSBridge.invoke('closeWindow')
                  }
                })
              }
            })
          })
        }, () => {})
      })
    },
    methods: {
    }
  }
</script>
<style lang="less">
#wechatticket {
  width: 100%;
  height: 100%;
  text-align: center;
  position: fixed;
  top: 50%;
  img {
    margin-top: -90px;
  }
}
</style>
